import requests
from loguru import logger
from ..config.douyin_config import DouyinConfig

class DouyinAPI:
    def __init__(self, account_id: str, cookies: dict):
        self.account_id = account_id
        self.cookies = cookies
        self.config = DouyinConfig()

    def get_following_rooms(self) -> list:
        """获取正在直播的关注列表"""
        try:
            response = requests.get(
                url="https://www.douyin.com/webcast/web/feed/follow/",
                headers=self.config.HEADERS,
                cookies=self.cookies,
                params=self.config.PARAMS
            )
            
            if response.status_code == 200:
                data = response.json()
                return [
                    f"https://live.douyin.com/{i['web_rid']}" 
                    for i in data["data"]["data"]
                ]
            return []
            
        except Exception as e:
            logger.error(f"获取关注直播列表失败 {self.account_id}: {e}")
            return []

    def add_rooms_to_monitor(self, room_list: list) -> bool:
        """添加房间到监控列表"""
        try:
            if not room_list:
                return False
                
            logger.info(f'{self.account_id} 发送add请求')
            response = requests.post(
                url="http://127.0.0.1:25508/add_url/",
                json={"urls": room_list},
                verify=False
            )
            return response.status_code == 200
            
        except Exception as e:
            logger.error(f"添加房间到监控失败 {self.account_id}: {e}")
            return False 